<template>
	<view style='padding-top:30vh'>
		<u-empty mode="permission" :text='tips' iconColor='#19be6b' @click='pay'>
		</u-empty>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				apiUrl: getApp().globalData.apiBase,
				tips: '正在检查支付环境安全性...',
				invoice: 0
			}
		},
		onLoad(options) {
			if (options.hasOwnProperty('invoice')) {
				let invoice = options.invoice
				if (/AlipayClient/i.test(navigator.userAgent)) {
					// 支付宝扫码打开的，直接在本页面调起支付并调整到支付宝的支付界面
					uni.setStorageSync('api_token', 'a8946b91-7b66-40c9-8982-f2b1be8775d2')
					this.tips = '支付环境安全,正在唤起支付宝支付....'
					this.invoice = invoice
					this.pay()
				} else {
					this.tips = '支付环境安全，正在调起微信支付....'
					// 微信的，需要进行微信登录,并且另外装一个界面进行回
					let url = `${this.apiUrl}h5/pages/payment/index?invoice=${invoice}`
					this.request.get(`wechat/official/generate-auth-url`, {
						'redirect_url': `${url}`
					}).then(res => {
						window.location.href = res.data
					}).catch((err) => {
						uni.showModal({
							title: '提示',
							content: JSON.stringify(err),
							showCancel: false,
							success: () => {}
						})
					})
				}
			} else {
				uni.showModal({
					title: '',
					content: '支付二维码失效，请重新扫码',
					showCancel: false,
					success: () => {

					}
				})
			}
		},
		onShow() {},
		methods: {
			pay() {
				uni.showLoading()
				this.request.post(`api/invoices/${this.invoice}/pay/huifu`, {
					gateway: 'jspay',
					trade_type: 'A_NATIVE'
				}).then((pay) => {
					uni.hideLoading()
					window.location.href = pay.data.payment.qr_code
				}).catch(()=>{
					uni.hideLoading()
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	
</style>